//
//  HGScanner.m
//  HGMM
//
//  Created by @HUI on 15/9/16.
//  Copyright (c) 2015年 cc. All rights reserved.
//

#import "HGScanner.h"
#import "HGPluginViewController.h"
#import "CCLog.h"
#import "HGPluginUtil.h"
#import "NSString+hui.h"
#import "QRcodeViewController.h"
#import "UIView+ViewController.h"
#import "QRCodeShowViewController.h"
#import "LYYNavigationController.h"
#import "NSArray+Json.h"

@implementation HGScanner

-(void)scannerQR:(CDVInvokedUrlCommand *)command{
    HGPluginViewController *vc;
    if ([self.viewController isKindOfClass:[HGPluginViewController class]]) {
        vc = (HGPluginViewController *)self.viewController;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            __block CDVPluginResult *pluginResult = nil;
            /*
            
            ZCZBarViewController *zc = [[ZCZBarViewController alloc]initWithBlock:^(NSString *result, BOOL success) {
                
                if (!success) {
                    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"扫描失败"];
                }else{
                    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:result];
                }
                
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            }];
            
            [vc.rootViewController presentViewController:zc animated:YES completion:^{
                
            }];
             */
            
            QRcodeViewController *qrCodeVC = [[QRcodeViewController alloc]init];
            [self.webView.viewContoller.navigationController pushViewController:qrCodeVC animated:YES];
            
            qrCodeVC.resultBlock = ^(NSString *result) {
                
                if (result.length > 0)
                {
                    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:result];
                }
                else
                {
                      pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"扫描失败"];
                }
                
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            
            };
            
            
        });
    }
}

-(void)createQR:(CDVInvokedUrlCommand *)command{
    if (command.arguments.count != 1 || ![[command.arguments objectAtIndex:0] isKindOfClass:[NSString class]]) {
        id tmp = [command.arguments objectAtIndex:0];
        CCLog(@"HGScanner_createQR 参数不正确: %@", tmp);
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"args is wrong!"];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        return ;
    }
    
    
    
    QRCodeShowViewController *qrCodeVC = [[QRCodeShowViewController alloc]init];
    qrCodeVC.qrCodeContent = command.arguments[0]; //@"邢伟新";
    LYYNavigationController *lyy = [[LYYNavigationController alloc]initWithRootViewController:qrCodeVC];
    [self.webView.viewContoller presentViewController:lyy animated:YES completion:nil];
    
    qrCodeVC.imageBlock = ^(NSArray * _Nonnull array) {
        
        
        if (array.count > 0)
        {
            NSArray *result = [self saveImageWithImages:array pluginId:Plugin_IN path:@"la/cc"];
            NSString *resultString = [result toReadableJSONString];
            CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:resultString];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            
        }
        else
        {
           CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"生成二维码失败"];
            
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
        
       
        
    };
    
    
    
   
    
    
    
}


-(NSArray *)saveImageWithImages:(NSArray *)imageArr  pluginId:(NSString *)pluginId path:(NSString *)path{
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
    NSString *imgName = [NSString stringWithCurrentDate];
    HGPluginUtil *util = [[HGPluginUtil alloc]init];
    NSString *rootPath = [[NSString stringWithFormat:@"%@/image",[util getPluginDataDir:pluginId]]stringByAppendingPathComponent:path];
    NSError *error = nil;
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:rootPath]) {
        [manager createDirectoryAtPath:rootPath withIntermediateDirectories:YES attributes:nil error:&error];
        if (error) {
            return nil;
        }
    }
    for (int i = 0; i < imageArr.count; i++) {
        NSString *imgPath = [NSString stringWithFormat:@"%@_%d.jpg",imgName, i];
        NSString *savePath = [rootPath stringByAppendingPathComponent:imgPath];
        NSData *imgData = UIImageJPEGRepresentation(imageArr[i], 1);
        [imgData writeToFile:savePath atomically:YES];
        [arr addObject:savePath];
    }
    
    return arr;
}


@end
